package com.mouliu.base.concurrent17;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author mouliu
 * @create 2018-04-15-下午4:35
 */
public class DiscardPolicy {
    public static void main(String[] args){
        // 创建线程池。线程池的"最大池大小"和"核心池大小"都为1，"线程池"的阻塞队列容量为1。
        ThreadPoolExecutor poolExecutor=
                new ThreadPoolExecutor(1,1,0,
                        TimeUnit.SECONDS,new ArrayBlockingQueue<>(1));
        //设置线程策略
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        //新建10个任务，并将它们添加到线程中
        for (int i=0;i<10;i++){
            Runnable mytask =
                    new MyTask(i,"running"+i);
            poolExecutor.execute(mytask);
        }
        //关闭线程
    }
}
